﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Maticsoft.Common;

namespace Maticsoft.Web.wap
{
    public partial class ProductList : BasePage
    {
        int CategoryId = 0;
        int CurrentPage = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["cid"], out CategoryId);
            if (!int.TryParse(Request.QueryString["page"], out CurrentPage))
            {
                CurrentPage = 1;
            }
            hidCategory.Value = CategoryId.ToString();
            hidPage.Value = CurrentPage.ToString();

            AjaxProcess();

            if (!IsPostBack)
            {
                GetCategory();
            }
        }

        protected void AjaxProcess()
        {
            if (Request.QueryString["sync"] == "true")
            {
                Response.Write(GetListJson());
                Response.End();
            }
        }

        protected string GetListJson()
        {
            BLL.Products proBLL = new BLL.Products();
            string strWhere = "(categoryId=" + CategoryId + " or categoryId in(select categoryId from Categorys where parentId=" + CategoryId + ")) and IsHid=0 and IsActive=1";
            string strOrder = "ProductId desc";
            int recordCount = proBLL.GetRecordCount(strWhere);
            int startIndex = 0;
            int endIndex = 0;
            int total = 0;
            if (recordCount <= 10)
            {
                total = 1;
                startIndex = 1;
                endIndex = recordCount;
            }
            else
            {
                total = recordCount % 10 == 0 ? recordCount / 10 : recordCount / 10 + 1;
                startIndex = 10 * (CurrentPage - 1) + 1;
                endIndex = 10 * CurrentPage;
            }
            DataSet ds = proBLL.GetListByPage(strWhere, strOrder, startIndex, endIndex);
            StringBuilder strBuilder = new StringBuilder();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];
                if (strBuilder.ToString() != "")
                {
                    strBuilder.Append(",");
                }
                strBuilder.Append("{\"id\":\"" + dr["ProductId"] + "\",\"name\":\"" + dr["ProductName"] + "\",\"pic\":\"" + dr["Pic"] + "\",\"price\":\"" + DigitHelper.GetDecimal(dr["SalePrice"]).ToString("f2") + "\"}");
            }

            return "{\"total\":\"" + total + "\",\"currPage\":\"" + CurrentPage + "\",\"list\":[" + strBuilder.ToString() + "]}";
        }

        protected void GetCategory()
        {
            BLL.Categorys catBLL = new BLL.Categorys();
            Model.Categorys catModel = catBLL.GetModel(CategoryId);
            hTitle.InnerText = catModel.CategoryName;
        }
    }
}